package pl.edu.mimuw.irs.core.util;

import java.io.Serializable;


/**
 * @author Jakub Rauch
 * Created on: 2009-05-08
 */
public class Position implements Serializable {

	private static final long serialVersionUID = 7304843493659916989L;
	
	public static final int STEP = 25;
	
	// *** Static methods
	
	public static Position getAverage(Position[] positions) {
		int avgX = 0;
		int avgY = 0;
		
		for (Position position : positions) {
			avgX += position.getX();
			avgY += position.getY();
		}
		
		return new Position(avgX / positions.length, avgY / positions.length);
	}
	
	// *** Attributes
	
	// Coordinates
	private int x;
	private int y;
	// Getters and setters
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	
	// *** Methods
	
	public Position(int x, int y) {
		setX(x);
		setY(y);
	}
	
	public Position() {
		this(0, 0);
	}
	
	public Position copy() {
		return new Position(this.getX(), this.getY());
	}
	
	public Position sub(Position position) {
		int x = getX() - position.getX();
		int y = getY() - position.getY();
		return new Position(x, y);
	}

	public Position sum(Position position) {
		int x = getX() + position.getX();
		int y = getY() + position.getY();
		return new Position(x, y);
	}
	
	public Position under(int i) {
		return new Position(getX(), getY() + i * STEP);
	}
	
	public Position under() {
		return under(1);
	}

	public Position over(int i) {
		return new Position(getX(), getY() - i * STEP);
	}
	
	public Position over() {
		return over(1);
	}
	
	public Position left(int i) {
		return new Position(getX() - i * STEP, getY());
	}
	
	public Position left() {
		return left(1);
	}

	public Position right(int i) {
		return new Position(getX() + i * STEP, getY());
	}
	
	public Position right() {
		return right(1);
	}
	
}
